﻿using System;
using System.Windows.Forms;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public partial class MainForm
    {
        private void OnMustRecalculateDiscrepancies(RowTag[] pRowTags, Weekday[] pDays)
        {
            if (pRowTags == null ||
                pRowTags.Length == 0 ||
                CurrentTemplate == null ||
                CurrentRota == null)
            {
                return;
            }
            
            foreach (var day in pDays)
            {
                DiscrepancyHunter.MarkOddAsOdd(
                    CurrentRota.GetAllShiftsForColumn(day.ToInt1()),
                    CurrentTemplate.GetAllShiftsForColumn(day.ToInt1()));
                var mode = (pRowTags[0].MyType == RowTagType.LongInt) ?
                    SheetType.Rota :
                    SheetType.Template;
                foreach (var tag in pRowTags)
                {
                    if (mode == SheetType.Template &&
                        CurrentTemplate[tag] != null) // it could be null, if the row has just been removed!
                    {                        
                        CurrentTemplate[tag][day.ToInt1()].Update();
                    }
                    if (mode == SheetType.Rota)
                    {
                        CurrentRota[tag][day.ToInt1()].Update();
                    }
                }
                if (mode == SheetType.Rota)
                {
                    CurrentTemplate.RefreshColumn(day);
                }
                else
                {
                    CurrentRota.RefreshColumn(day);
                }                
            }
            lbxDiscrepancies.Text = CurrentDiscrepancy.StringRepresentation().Replace(
                "#",
                CurrentTemplate.Name);
        }

        /// <summary>
        /// If rota was modified,
        /// </summary>
        /// <returns></returns>
        private bool IsUserOKWithLeavingCurrentRota()
        {
            if (CurrentRota == null)
            {
                return true;
            }
            if (!CurrentRota.Modified)
            {
                return true;
            }
            var caption = Resources.MESSAGEBOX_LeavingRotaCaption;
            var message = Resources.MESSAGEBOX_LeavingRotaMessage.Replace(@"\n", Environment.NewLine);
            caption = caption.Replace(
                "#",
                CurrentRota.bndDays);
            message = message.Replace(
                "#",
                CurrentRota.bndDays);

            var decision = MessageBox.Show(
                message,
                caption,
                MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Question);
            if (decision == DialogResult.Cancel)
            {
                return false;
            }
            if (decision == DialogResult.Yes)
            {
                Program.CurrentFile.ReuniteWithDatabase(CurrentRota);
            }
            return true;                
        }

        private bool IsUserOKWithLeavingCurrentTemplate()
        {            
            /* My application saves modified templates automatically.
             * This behaviour could be modified if needed -
             * in this very method, which serves as a placeholder.
             * 
             * Saving templates without confirmation was my choice - */
            Program.CurrentFile.ReuniteWithDatabase(CurrentTemplate);
            return true;
        }

        private void sheetRota_OnRefreshTotalHours(SumOfHours? pTotalHours, SumOfHours? pTotalContractHours)
        {
            var totalHasValue = pTotalHours.HasValue;
            var contractHasValue = pTotalContractHours.HasValue;
            if (!(totalHasValue | contractHasValue))
            {
                return;
            }
            tblRotaToolbar2.SuspendLayout();

            if (totalHasValue &&
                lbTotalHrs.Text != pTotalHours.Value.ToString())
            {
                lbTotalHrs.Text = pTotalHours.Value.ToString();
            }
            if (contractHasValue &&
                lbContractHours.Text != pTotalContractHours.Value.ToString("contract"))
            {
                lbContractHours.Text = pTotalContractHours.Value.ToString("contract");
            }
            tblRotaToolbar2.ResumeLayout();
        }

        private void sheetTempRota_OnRefreshTotalHours(SumOfHours? pTotalHours, SumOfHours? pTotalContractHours)
        {
            if (!pTotalHours.HasValue)
            {
                return;
            }
            tblTempToolbar.SuspendLayout();            
            if (lbTempTotalHrs.Text != pTotalHours.Value.ToString())
            {
                lbTempTotalHrs.Text = pTotalHours.Value.ToString();
            }
            tblTempToolbar.ResumeLayout();
        }
    }
}
